// stdx/panicx.rs
// CRITICAL: panic! 与 catch_unwind 示例。注意：catch_unwind 只能捕获 panic!，不可捕获别的异常。
// AUTO_COMMENT: 只作为教育示例。

#[allow(dead_code)]
pub fn stdx_examples_panicx() {
    use std::panic;
    let _ = panic::catch_unwind(|| {
        // do nothing
    });

    // CRITICAL: panic! 宏
    // panic!("This is a panic!"); // 注释掉以避免实际 panic

    // CRITICAL: catch_unwind 捕获 panic
    let result = panic::catch_unwind(|| {
        panic!("Something went wrong!");
    });
    assert!(result.is_err());

    // CRITICAL: set_hook 设置 panic hook
    let old_hook = panic::take_hook();
    panic::set_hook(Box::new(|info| {
        println!("Custom panic hook: {:?}", info);
    }));
    // 恢复 hook
    panic::set_hook(old_hook);

    // CRITICAL: panic_any 抛出任意类型
    // panic::panic_any(42); // 注释掉

    // CRITICAL: resume_unwind 重新抛出
    let result2 = panic::catch_unwind(|| {
        panic::resume_unwind(Box::new("resumed"));
    });
    assert!(result2.is_err());

    let _ = (result, result2);
}
